package cn.yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.yunhe.biz.IProviencesBiz;
import cn.yunhe.biz.IUserBiz;
import cn.yunhe.biz.ProvienceBizImpl;
import cn.yunhe.biz.UserBizImpl;
import cn.yunhe.entity.Proviences;
import cn.yunhe.entity.User;

/**
 * Servlet implementation class DoUser
 */
public class DoUser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoUser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
		HttpSession session = request.getSession();
		PrintWriter out =response.getWriter();
		IUserBiz iub = new UserBizImpl();
		
		String opt = request.getParameter("opt");
		if("add".equals(opt)){
			String email = request.getParameter("email");
			String username = request.getParameter("username");
			String pwd = request.getParameter("pwd");
			String province = request.getParameter("province");
			String wenorli = request.getParameter("wenorli");
			String phon = request.getParameter("phon");
			
			int wri = Integer.parseInt(wenorli);
			
			
			User user = new User();
			user.setEmail(email);
			user.setUsername(username);
			user.setProvinces(province);
			user.setWenorli(wri);
			user.setUserphone(phon);
			user.setPwd(pwd);
		
			boolean r = iub.addUser(username, pwd, email, phon, province, wri);
			response.sendRedirect("DoBackstage?opt=getUser");
		}else if("update".equals(opt)){
			String suserid = request.getParameter("userid");
			String email = request.getParameter("email");
			String username = request.getParameter("username");
			String pwd = request.getParameter("pwd");
			String provinces = request.getParameter("province");
			String wenorli = request.getParameter("wenorli");
			String phon = request.getParameter("phon");
			int userid = Integer.parseInt(suserid);
			int wri = Integer.parseInt(wenorli);
			String sql = "update gaokaouser set email=?,username=?,pwd=?,provinces=?,wenorli=?,userphone=?where userid=?";
			Object[] pramas={email,username,pwd,provinces,wenorli,phon,userid};
			boolean r= iub.updateUser(sql, pramas);
			response.sendRedirect("DoBackstage?opt=getUser");
		}else if("del".equals(opt)){
			String suserid = request.getParameter("userid");
			int userid = Integer.parseInt(suserid);
			boolean r = iub.delUser(userid);
			response.sendRedirect("DoBackstage?opt=getUser");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
